package com.dailyblue.java.project.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.java.project.user.bean.Users;
import com.dailyblue.java.project.user.config.JwtConfig;
import com.dailyblue.java.project.user.mapper.UsersMapper;
import com.dailyblue.java.project.user.service.UsersService;
import com.dailyblue.java.project.user.util.JsonResult;
import com.dailyblue.java.project.user.util.ResultCode;
import com.dailyblue.java.project.user.util.ResultTool;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private UsersMapper usersMapper;

    public JsonResult login(Users user) {
        // 校验账号和密码是否正确
        // 1.正确
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("user_code", user.getUserCode());
        Users user1 = usersMapper.selectOne(wrapper);
        // 登陆失败 用户没有找到
        if (user1 == null) {
            return ResultTool.fail(ResultCode.USER_ACCOUNT_NOT_EXIST);
        }
        // 校验密码是否正确
        String pass1 = user1.getPassword(); // 数据库的密码
        String pass2 = user.getPassword(); // 用户传过来的密码
        log.info("数据库密码：{}", pass1);
        log.info("传递过来的密码：{}", pass2);
        // 登陆失败 密码错误
        if (!pass1.equals(pass2)) {
            return ResultTool.fail(ResultCode.USER_CREDENTIALS_ERROR);
        }
        // 1.1生成token
        String token = JwtConfig.getJwtToken(user1);
        // 1.2存放到Redis中
        stringRedisTemplate.opsForValue().set("token:" + user1.getId(), token, 1, TimeUnit.DAYS);
        // 1.3返回
        JsonResult result = ResultTool.success(token);
        result.setMessage(user1.getNickName());
        return result;
    }

    public JsonResult isLogin(String token) {
        // 1.用户没有传递token
        if (token == null) {
            return ResultTool.fail(ResultCode.USER_NOT_LOGIN);
        }
        // 2.用户传递过来的token是不合法的
        boolean b = JwtConfig.checkToken(token);
        if (!b) {
            return ResultTool.fail(ResultCode.USER_NOT_LOGIN);
        }
        // 3.用户token和Redis中存放的token不一致
        Claims claims = JwtConfig.parseJWT(token);
        String id = claims.get("id").toString();
        ValueOperations<String, String> value = stringRedisTemplate.opsForValue();
        String redisToken = value.get("token:" + id);
        if (!token.equals(redisToken)) {
            return ResultTool.fail(ResultCode.USER_NOT_LOGIN);
        }
        return ResultTool.success(claims.get("nick"));
    }

    @Override
    public JsonResult getUserId(String token) {
        if (token == null) {
            return ResultTool.fail(ResultCode.COMMON_FAIL);
        }
        Claims claims = JwtConfig.parseJWT(token);
        return ResultTool.success(claims.get("id"));
    }
}
